#include<iostream>
#include<string>
#include<vector>
#include<list>
#include<algorithm>
#include"reverse_iterator.h"
using namespace std;
#pragma once
#include<assert.h>

namespace yjt
{
	template<class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;

		typedef ReverseIterator<iterator, T&, T*> reverse_iterator;
		typedef ReverseIterator<const_iterator, const T&, const T*>const_reverse_iterator;

		vector()
		{}

		vector(vector<T>& v)
		{
			_start = new T[v.capacity()];
			memcpy(_start, v._start, v.size() * sizeof(T));
			_finish = _start + v.size();
			_endofstorage = _start + v.capacity();
		}
		reverse_iterator rbegin()
		{
			return reverse_iterator(end());
		}
		reverse_iterator rend()
		{
			return reverse_iterator(begin());
		}
		const_reverse_iterator rbegin()const
		{
			return const_reverse_iterator(end());
		}
		const_reverse_iterator rend()const
		{
			return const_reverse_iterator(begin());
		}
		iterator begin()
		{
			return _start;
		}

		iterator end()
		{
			return _finish;
		}

		const_iterator begin() const
		{
			return _start;
		}

		const_iterator end() const
		{
			return _finish;
		}

		// 2010
		void reserve(size_t n)
		{
			if (n > capacity())
			{
				size_t old = size();
				T* tmp = new T[n];
				if (_start)
				{
					memcpy(tmp, _start, old * sizeof(T));
					delete[] _start;
				}

				_start = tmp;
				_finish = _start + old;
				_endofstorage = _start + n;
			}
		}
		void push_back(const T& x)
		{
			if (_finish == _endofstorage)
			{
				size_t newcapacity = capacity() == 0 ? 4 : capacity() * 2;
				reserve(newcapacity);
			}

			*_finish = x;
			++_finish;
		}
		size_t size() const
		{
			return _finish - _start;
		}

		size_t capacity() const
		{
			return _endofstorage - _start;
		}
		void pop_back()
		{
			--_finish;
		}
		void insert(size_t pos, const T& x)
		{
			if (_finish == _endofstorage)
			{
				size_t pos = pos - _start;
				size_t newcapacity = capacity() == 0 ? 4 : capacity() * 2;
				reserve(newcapacity);
				pos = _start + len;
			}
			memmove(pos + 1, pos, sizeof(T) * (_finish - pos));
			*pos = x;
			++_finish;
		}
		T* operator[](size_t pos)
		{
			return _start[pos];
		}
	private:
		iterator _start = nullptr;
		iterator _finish = nullptr;
		iterator _endofstorage = nullptr;
	};

	void print_vector(const vector<int>& v)
	{
		for (auto e : v)
		{
			cout << e << " ";
		}
		cout << endl;
	}

	void text()
	{
		vector<int> v;
		v.push_back(1);
		v.push_back(2);
		v.push_back(3);
		v.push_back(4);
		v.push_back(4);
		v.push_back(4);
		v.push_back(4);
	}
}
	
